﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using ADSTS_SERVER.Models;
using ADSTS_SERVER.ViewModels.Project;
using ADSTS_SERVER.ViewModels.Robot;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using ADSTS_SERVER.ViewModels.RoadMap;

namespace ADSTS_SERVER.Controllers
{
    /// <summary>
    /// 路线
    /// </summary>
    [DynamicApiController]
    public class LnsRoadMapController
    {
        private readonly ILogger<LnsProjectController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public LnsRoadMapController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 路线列表
        /// </summary>
        /// <param name="rsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] RoadMapSearchForm rsf)
        {
            RefAsync<int> total = 0;
            List<RoadMap> roadmaps = await db.Queryable<RoadMap>()
                .Where(rd => rd.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(rsf.Content), rd => rd.Name.Contains(rsf.Content))
                .ToPageListAsync(rsf.PageIndex, rsf.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = roadmaps,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加路线
        /// </summary>
        /// <param name="psf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] RoadMapSaveForm vo)
        {
            await db.Insertable<RoadMap>(vo.Adapt<RoadMap>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除路线
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] List<long> ids) {
            await db.Updateable<RoadMap>(new RoadMap { IsActive = 0 }).UpdateColumns(pj => pj.IsActive).Where(pj => ids.Contains(pj.Id)).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改路线
        /// </summary>
        /// <param name="vo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] RoadMapUpdateForm vo) {
            await db.Updateable<RoadMap>(vo).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = vo,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看路线
        /// </summary>
        /// <param name="psf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] RoadMapShowForm psf) {
            RoadMap roadmap = await db.Queryable<RoadMap>()
                .InSingleAsync(psf.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = roadmap,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
